<script setup lang="ts">
  import { computed } from 'vue';
  import { $t } from '@/locales';
  import { useFormRules, useNaiveForm } from '@/hooks/common/form';
  import { enableStatusOptions, userGenderOptions } from '@/constants/business';
  import { translateOptions } from '@/utils/common';
  import type { Api } from '@/typings/api';
  defineOptions({
    name: 'UserSearch',
  });

  interface Emits {
    (e: 'reset'): void;
    (e: 'search'): void;
  }

  const emit = defineEmits<Emits>();

  const { formRef, validate, restoreValidation } = useNaiveForm();

  const model: any = defineModel<Api.SystemManage.UserSearchParams>('model', {
    required: true,
  });

  type RuleKey = Extract<
    keyof Api.SystemManage.UserSearchParams,
    'email' | 'phone'
  >;

  const rules = computed<Record<RuleKey, App.Global.FormRule>>(() => {
    const { patternRules } = useFormRules(); // inside computed to make locale reactive

    return {
      email: patternRules.email,
      phone: patternRules.phone,
    };
  });

  async function reset() {
    await restoreValidation();
    emit('reset');
  }

  async function search() {
    await validate();
    emit('search');
  }
</script>

<template>
  <NCard :bordered="false" size="small" class="card-wrapper">
    <NCollapse>
      <NCollapseItem :title="$t('common.search')" name="user-search">
        <NForm
          ref="formRef"
          :model="model"
          :rules="rules"
          label-placement="left"
          :label-width="80"
        >
          <NGrid responsive="screen" item-responsive>
            <NFormItemGi
              span="24 s:12 m:6"
              :label="$t('page.userData.userList.userId')"
              path="userId"
              class="pr-24px"
            >
              <NInput
                v-model:value="model.operatorId"
                clearable
                :placeholder="$t('page.userData.userListForm.userId')"
              />
            </NFormItemGi>
            <!--
 <NFormItemGi span="24 s:12 m:6" :label="$t('page.userData.userList.name')" path="name" class="pr-24px">
              <NInput v-model:value="model.name" :placeholder="$t('page.userData.userListForm.name')" />
            </NFormItemGi>
            <NFormItemGi span="24 s:12 m:6" :label="$t('page.userData.userList.phone')" path="phone" class="pr-24px">
              <NInput v-model:value="model.phone" :placeholder="$t('page.userData.userListForm.phone')" />
            </NFormItemGi>
-->
            <!--
 <NFormItemGi span="24 s:12 m:6" :label="$t('page.userData.userList.email')" path="email" class="pr-24px">
              <NInput v-model:value="model.email" :placeholder="$t('page.userData.userListForm.email')" />
            </NFormItemGi>
-->
            <!--
 <NFormItemGi
              span="24 s:12 m:6"
              :label="$t('page.manage.user.userStatus')"
              path="userStatus"
              class="pr-24px"
            >
              <NSelect
                v-model:value="model.status"
                :placeholder="$t('page.manage.user.form.userStatus')"
                :options="translateOptions(enableStatusOptions)"
                clearable
              />
            </NFormItemGi>
-->
            <NFormItemGi span="24 m:12" class="pr-24px">
              <NSpace class="w-full" justify="end">
                <NButton @click="reset">
                  <template #icon>
                    <icon-ic-round-refresh class="text-icon" />
                  </template>
                  {{ $t('common.reset') }}
                </NButton>
                <NButton type="primary" ghost @click="search">
                  <template #icon>
                    <icon-ic-round-search class="text-icon" />
                  </template>
                  {{ $t('common.search') }}
                </NButton>
              </NSpace>
            </NFormItemGi>
          </NGrid>
        </NForm>
      </NCollapseItem>
    </NCollapse>
  </NCard>
</template>

<style scoped></style>
